package com.service.impl;

import com.dao.ISupplierBlackListDao;
import com.dao.impl.SupplierBlackListDaoImpl;
import com.pojo.SupplierBlackList;

import com.service.ISupplierBlackListService;
import com.util.PageHelper;

import java.util.ArrayList;

public class SupplierBlackListServciceImpl implements ISupplierBlackListService {

    ISupplierBlackListDao isbd = new SupplierBlackListDaoImpl();

    // 1. 添加供应商到黑名单
    @Override
    public boolean addBlackList(SupplierBlackList blackList) {
        return isbd.insertBlackList(blackList) > 0;
    }

    // 分页查询企业的黑名单列表
    @Override
    public PageHelper getBlackListByPage(Integer enterpriseId, Integer pageIndex) {
        PageHelper ph = new PageHelper();
        // 1. 查询总条数
        int totalCount = isbd.selectBlackListCount(enterpriseId);
        ph.setDataCount(totalCount);
        // 2. 设置当前页
        if (pageIndex != null) {
            ph.setPageIndex(pageIndex);
        } else {
            ph.setPageIndex(1);
        }
        // 3. 计算起始索引
        int start = (pageIndex - 1) * 5;
        // 4. 查询当前页数据
        ArrayList<SupplierBlackList> blackList = isbd.selectBlackListByEnterpriseId(enterpriseId, pageIndex);
        ph.setListIndex(blackList);
        return ph;

    }


// 删除黑名单
@Override
public boolean delBlackList(Integer enterpriseId, Integer supplierId) {
    return isbd.deleteBlackList(enterpriseId, supplierId) > 0;
}

//   检查企业是否已在黑名单中
@Override
public boolean isInBlackList(Integer enterpriseId, Integer supplierId) {
    return isbd.checkInBlackList(enterpriseId, supplierId);
}

// 按条件分页查询黑名单（企业名称模糊查询、企业评级）
@Override
public PageHelper searchBlackListByCondition(Integer enterpriseId, String supplierName, String supplierLevel, Integer pageIndex) {
    PageHelper ph1 = new PageHelper();
    //获取按条件查询的总条数
    int dataCount = isbd.selectBlackListCountByCondition(enterpriseId, supplierName, supplierLevel);
    //根据企业id和条件查供应商黑名单数据
    ArrayList<SupplierBlackList> supplierBlackLists = isbd.selectBlackListByCondition(enterpriseId, supplierName, supplierLevel, pageIndex);
    ph1.setDataCount(dataCount);
    ph1.setListIndex(supplierBlackLists);

    if (pageIndex != null) {
        //获取当前页
        ph1.setPageIndex(pageIndex);
    } else {
        ph1.setPageIndex(1);
    }
    return ph1;
}

}

